#include "picture.h"
#include <QLabel>
#include <QLineEdit>
#include <QComboBox>
#include <QPushButton>
#include <QGridLayout>
#include <QString>
#include <QTimer>
#include <QList>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <qdebug.h>
#include <qfile.h>
#include <QFile>
#include <QtGui>
#include <QDebug>
#include <qmainwindow.h>
#include "configargs.h"

#include <QMainWindow>
#include <QPrinter>
#include <QString>
#include <unistd.h>

static int butSize_H = 50;
static int butSize_V = 50;

PictureWidget::PictureWidget(QWidget *parent) :QDialog(parent)
{
    DebugOrRelease=0;
    if(g_lcdsize == LCDSIZE_1280x800){
        m_DeskFPixMap.load(":/images/background_1280x800.png");
        butSize_H=80;
         butSize_V=80;
    }else if(g_lcdsize == LCDSIZE_1024x768){
             m_DeskFPixMap.load(":/images/background_1024x768.png");
             butSize_H=80;
              butSize_V=80;
    }else if(g_lcdsize == LCDSIZE_800x600){
		m_DeskFPixMap.load(":/images/background_800x600.png");
		butSize_H=65;
		butSize_V=65;

    }else   if(g_lcdsize == LCDSIZE_800x480){		 	
		m_DeskFPixMap.load(":/images/background_800x480.png");
		butSize_H=50;
		butSize_V=50;

    }else   if(g_lcdsize == LCDSIZE_480x272){		 	
		m_DeskFPixMap.load(":/images/background_480x272.png");
		butSize_H=30;
		butSize_V=50;

    }
    countnet2=0;
    goodCount=0;
    nCount=0;
    nowCount=0;
    listWidget = new QListWidget;
    m_flag =0;
	fullscreen_flag = 0; //标志全屏，假如全屏显示则在全屏基础上缩放，否则在原图基础上缩放
	
    pOpenButton              = new QPushButton;
    pZinButton              = new QPushButton;
    pZoutButton              = new QPushButton;
    pNorButton              = new QPushButton;
    pNextButton              = new QPushButton;
    pPreviouButton              = new QPushButton;
    pFitButton               = new QPushButton;
    pWcloseButton                = new QPushButton;
    pOpenFileButton               = new QPushButton;
    pDeskButton               = new QPushButton;
    setMouseTracking(true);
    pOpenFileButton->setGeometry(0,0,butSize_H,butSize_V); //按钮的位置及大小;
    pOpenFileButton->clearMask();
    pOpenFileButton->setBackgroundRole( QPalette::Base);


    pNextButton->setGeometry(0,1,butSize_H,butSize_V); //按钮的位置及大小
    pNextButton->clearMask();
    pNextButton->setBackgroundRole( QPalette::Base);

    pPreviouButton->setGeometry(0,2,butSize_H,butSize_V); //按钮的位置及大小
    pPreviouButton->clearMask();
    pPreviouButton->setBackgroundRole( QPalette::Base);

    pZinButton->setGeometry(0,3,butSize_H,butSize_V); //按钮的位置及大小
    pZinButton->clearMask();
    pZinButton->setBackgroundRole( QPalette::Base);

    pZoutButton->setGeometry(0,4,butSize_H,butSize_V); //按钮的位置及大小
    pZoutButton->clearMask();
    pZoutButton->setBackgroundRole( QPalette::Base);

    pNorButton->setGeometry(0,5,butSize_H,butSize_V); //按钮的位置及大小
    pNorButton->clearMask();
    pNorButton->setBackgroundRole( QPalette::Base);

    pFitButton->setGeometry(0,6,butSize_H,butSize_V); //按钮的位置及大小
    pFitButton->clearMask();
    pFitButton->setBackgroundRole( QPalette::Base);

    pWcloseButton->setGeometry(0,7,butSize_H,butSize_V); //按钮的位置及大小
    pWcloseButton->clearMask();
    pWcloseButton->setBackgroundRole( QPalette::Base);

    pDeskButton->setGeometry(0,8,butSize_H,butSize_V); //按钮的位置及大小
    pDeskButton->clearMask();
    pDeskButton->setBackgroundRole( QPalette::Base);

	m_movie = new QMovie();
    pimageLabel = new QLabel;
    pimageLabel->setBackgroundRole(QPalette::Base);
    pimageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
	pimageLabel->setGeometry(0,0,m_DeskFPixMap.width()-6,m_DeskFPixMap.height()-6);
  

  //  pimageLabel->setGeometry(0,0,int_WIDGET_W-10,int_WIDGET_H-10);
    pimageLabel->setPixmap(m_DeskFPixMap);
    pimageLabel->setScaledContents(true);

    pZinButton		->setEnabled(false);
    pZoutButton		->setEnabled(false);
    pNorButton		->setEnabled(false);
    pNextButton		->setEnabled(false);
    pPreviouButton	->setEnabled(false);
    pFitButton		->setEnabled(false);
    pDeskButton->setEnabled(false);

    pOpenButton->setMinimumSize(120,120);
    pOpenButton->setMaximumSize(120,120);

   pOpenButton->setFocusPolicy(Qt::NoFocus);
   pOpenFileButton->setFocusPolicy(Qt::NoFocus);
   pZinButton->setFocusPolicy(Qt::NoFocus);
   pZoutButton->setFocusPolicy(Qt::NoFocus);
   pNorButton->setFocusPolicy(Qt::NoFocus);
   pNextButton->setFocusPolicy(Qt::NoFocus);
   pPreviouButton->setFocusPolicy(Qt::NoFocus);
   pFitButton->setFocusPolicy(Qt::NoFocus);
   pWcloseButton->setFocusPolicy(Qt::NoFocus);
   pDeskButton->setFocusPolicy(Qt::NoFocus);

/*
   pOpenFileButton->setFixedSize( m_OpenFilePixMap.width(), m_OpenFilePixMap.height() );
   pOpenFileButton->setMask(m_OpenFilePixMap.createHeuristicMask());
   pOpenFileButton->setIcon(m_OpenFilePixMap);
   pOpenFileButton->setIconSize(QSize(m_OpenFilePixMap.width(),m_OpenFilePixMap.height()));
   pOpenFileButton->setToolTip(tr("打开"));

   pNextButton->setFixedSize( m_NextPixMap.width(), m_NextPixMap.height() );
   pNextButton->setMask(m_NextPixMap.createHeuristicMask());
   pNextButton->setIcon(m_NextPixMap);
   pNextButton->setIconSize(QSize(m_NextPixMap.width(),m_NextPixMap.height()));
   pNextButton->setToolTip(tr("下一张"));

   pPreviouButton->setFixedSize( m_PrePixMap.width(), m_PrePixMap.height() );
   pPreviouButton->setMask(m_PrePixMap.createHeuristicMask());
   pPreviouButton->setIcon(m_PrePixMap);
   pPreviouButton->setIconSize(QSize(m_PrePixMap.width(),m_PrePixMap.height()));
   pPreviouButton->setToolTip(tr("上一张"));

   pZinButton->setFixedSize( m_ZinPixMap.width(), m_ZinPixMap.height() );
   pZinButton->setMask(m_ZinPixMap.createHeuristicMask());
   pZinButton->setIcon(m_ZinPixMap);
   pZinButton->setIconSize(QSize(m_ZinPixMap.width(),m_ZinPixMap.height()));
   pZinButton->setToolTip(tr("放大"));

   pZoutButton->setFixedSize( m_ZoutPixMap.width(), m_ZoutPixMap.height() );
   pZoutButton->setMask(m_ZoutPixMap.createHeuristicMask());
   pZoutButton->setIcon(m_ZoutPixMap);
   pZoutButton->setIconSize(QSize(m_ZoutPixMap.width(),m_ZoutPixMap.height()));
   pZoutButton->setToolTip(tr("缩小"));

   pNorButton->setFixedSize( m_NorPixMap.width(), m_NorPixMap.height() );
   pNorButton->setMask(m_NorPixMap.createHeuristicMask());
   pNorButton->setIcon(m_NorPixMap);
   pNorButton->setIconSize(QSize(m_NorPixMap.width(),m_NorPixMap.height()));
   pNorButton->setToolTip(tr("原大小"));

   pFitButton->setFixedSize( m_FitPixMap.width(), m_FitPixMap.height() );
   pFitButton->setMask(m_FitPixMap.createHeuristicMask());
   pFitButton->setIcon(m_FitPixMap);
   pFitButton->setIconSize(QSize(m_FitPixMap.width(),m_FitPixMap.height()));
   pFitButton->setToolTip(tr("充满"));

   pWcloseButton->setFixedSize( m_ClosePixMap.width(), m_ClosePixMap.height() );
   pWcloseButton->setMask(m_ClosePixMap.createHeuristicMask());
   pWcloseButton->setIcon(m_ClosePixMap);
   pWcloseButton->setIconSize(QSize(m_ClosePixMap.width(),m_ClosePixMap.height()));
   pWcloseButton->setToolTip(tr("关闭"));

   pDeskButton->setFixedSize( m_DeskPixMap.width(), m_DeskPixMap.height() );
   pDeskButton->setMask(m_DeskPixMap.createHeuristicMask());
   pDeskButton->setIcon(m_DeskPixMap);
   pDeskButton->setIconSize(QSize(m_DeskPixMap.width(),m_DeskPixMap.height()));
   pDeskButton->setToolTip(tr("隐藏 后单击移动鼠标"));

   pOpenButton->setStyleSheet("QPushButton{background-color:rgba(0,0,0,0);border-style:solid;border-width:0px;border-color rgba(0,0,0,0);}");
	if (DebugOrRelease)
  		this->setCursor(Qt::CrossCursor);
*/

     
   pOpenFileButton->setFixedSize(butSize_V, butSize_H);
   pOpenFileButton->setText(tr("打开"));
   pOpenFileButton->setStyleSheet(
					   "background-color: yellow;"
					   "selection-color: yellow;"
					   "selection-background-color: blue;");
   pOpenFileButton->setToolTip(tr("打开"));

   pNextButton->setFixedSize(butSize_V, butSize_H);
   pNextButton->setText(tr("下一张"));
//   pNextButton->setStyleSheet("background-color: green;");
   pNextButton->setToolTip(tr("下一张"));

   pPreviouButton->setFixedSize(butSize_V, butSize_H);
   pPreviouButton->setText(tr("上一张"));
//   pPreviouButton->setStyleSheet("background-color: green;");
   pPreviouButton->setToolTip(tr("上一张"));

   pZinButton->setFixedSize(butSize_V, butSize_H);
   pZinButton->setText(tr("放大"));
//   pZinButton->setStyleSheet("background-color: blue;");
   pZinButton->setToolTip(tr("放大"));

   pZoutButton->setFixedSize(butSize_V, butSize_H);
   pZoutButton->setText(tr("缩小"));
//   pZoutButton->setStyleSheet("background-color: blue;");
   pZoutButton->setToolTip(tr("缩小"));

   pNorButton->setFixedSize(butSize_V, butSize_H);
   pNorButton->setText(tr("原大小"));
//   pNorButton->setStyleSheet("background-color: blue;");
   pNorButton->setToolTip(tr("原大小"));

   pFitButton->setFixedSize(butSize_V, butSize_H);
   pFitButton->setText(tr("全屏"));
//   pFitButton->setStyleSheet("background-color: blue;");
   pFitButton->setToolTip(tr("充满"));

   pDeskButton->setFixedSize(butSize_V, butSize_H);
   pDeskButton->setText(tr("隐藏"));
//   pDeskButton->setStyleSheet("background-color: blue;");
   pDeskButton->setToolTip(tr("隐藏 后单击移动鼠标"));

   pWcloseButton->setFixedSize(butSize_V, butSize_H);
   pWcloseButton->setText(tr("退出"));
   pWcloseButton->setStyleSheet("background-color: red;");
   pWcloseButton->setToolTip(tr("关闭"));
   
   m_pTimer = new QTimer(this);
   m_pTimer->setInterval(4000);

   connect(m_pTimer, SIGNAL(timeout()), this, SLOT(autoNext()));

    pScrollArea = new QScrollArea;
    pScrollArea->setBackgroundRole(QPalette::Dark);

    pScrollArea->setWidget(pimageLabel);

    dirCurrent = new QDir();
    currentFile = displayFiles.constBegin();
    connect(pOpenFileButton, SIGNAL(clicked()), this, SLOT(open6()));
    connect(pZinButton, SIGNAL(clicked()), this, SLOT(zoomIn()));
    connect(pZoutButton, SIGNAL(clicked()), this, SLOT(zoomOut()));
    connect(pNorButton, SIGNAL(clicked()), this, SLOT(normalSize()));
    connect(pNextButton, SIGNAL(clicked()), this, SLOT(on_actionNext_triggered()));
    connect(pPreviouButton, SIGNAL(clicked()), this, SLOT(on_actionPrevious_triggered()));
    connect(pFitButton, SIGNAL(clicked()), this, SLOT(fitToWindow()));
    connect(pWcloseButton, SIGNAL(clicked()), this, SLOT(pictureClose()));
    connect(pDeskButton, SIGNAL(clicked()), this, SLOT(desk()));

    pMainLayout	= new QGridLayout;
    pMainLayout->setMargin(1);
    pMainLayout->setSpacing(1);

    pMainLayout->addWidget(pScrollArea,		    0, 0, 20, 20);
/*
    pMainLayout->addWidget(pOpenFileButton,		0,   19, 1, 1);
    pMainLayout->addWidget(pNextButton,			3, 19, 1, 1);
    pMainLayout->addWidget(pPreviouButton,		5, 19, 1, 1);
    pMainLayout->addWidget(pZinButton,		    7, 19, 1, 1);
    pMainLayout->addWidget(pZoutButton,		    9, 19, 1, 1);
    pMainLayout->addWidget(pNorButton,			11, 19, 1, 1);
    pMainLayout->addWidget(pFitButton,		    13, 19, 1, 1);
    pMainLayout->addWidget(pWcloseButton,		15, 19, 1, 1);
    pMainLayout->addWidget(pDeskButton,		    17, 19, 1, 1);
*/
	pMainLayout->addWidget(pOpenFileButton, 	0, 0, 1, 1);
	pMainLayout->addWidget(pNextButton, 		3, 0, 1, 1);
	pMainLayout->addWidget(pPreviouButton,		5, 0, 1, 1);
	pMainLayout->addWidget(pZinButton,			7, 0, 1, 1);
	pMainLayout->addWidget(pZoutButton, 		9, 0, 1, 1);
	pMainLayout->addWidget(pNorButton,			11, 0, 1, 1);
	pMainLayout->addWidget(pFitButton,			13, 0, 1, 1);
	pMainLayout->addWidget(pDeskButton, 		15, 0, 1, 1);
	pMainLayout->addWidget(pWcloseButton,		17, 0, 1, 1);




    this->setLayout(pMainLayout);

}

void PictureWidget::autoNext()
{
    on_actionNext_triggered();
}

void PictureWidget::open6()
{
    QFileDialog * fileDialogtmp = new QFileDialog();

    fileDialogtmp->setWindowState(Qt::WindowMaximized);
    fileDialogtmp->setMinimumSize(1280,800);
    goodCount=0;
    nCount=0;
	

    QString filePath;

    Folder2 * folderTools = new Folder2(this,2);

    folderTools->setGeometry(100, 100, int_WIDGET_W-150, int_WIDGET_H-150);
    folderTools->exec();

    QFile file1("/usrfs/qtapp/photo.conf");
    file1.open(QFile::ReadOnly);
    filePath=file1.readAll();
	
    file1.close();
    filePath.append("/");

	filePath.replace(QString("\n"), QString(""));
	qDebug()<<"pic filePath" <<filePath ;

    if(filePath.isNull()) {
        filePath="/photo/";
    }
    QDir dir(filePath);

    listWidget->clear();

    musicList = dir.entryList(QDir::Files,QDir::Name);   

    QString fileName;
   nCount = musicList.size();                           
 //  qDebug("ncount = %d",nCount);
   if(nCount != 0)
   fileName =  static_cast<QString>(musicList.at(0));   
   QString tmp="\n";
   QString tmp3=".jpg";
   QString tmp4=".png";
   QString tmp5=".gif";
   QString tmp6=".PNG";
   QString tmp7=".JPG";
   QString tmp8=".bmp";
   

   for(int i=0; i<nCount; i++) {

       QString tmp2;
       tmp2=static_cast<QString>(musicList.at(i));
 
        if(tmp2.contains(tmp3) || tmp2.contains(tmp4) || tmp2.contains(tmp5) || tmp2.contains(tmp6) || tmp2.contains(tmp7) || tmp2.contains(tmp8)){
       		listWidget->insertItem(i,new QListWidgetItem(QIcon(":/images/mp3.png"),(filePath + tmp2),listWidget));
       		goodCount++;
    
        }
   }

   qDebug("goodCount=%d",goodCount);

   for(int i=0; i<goodCount; i++) {

       listWidget->setCurrentRow(i);
       QListWidgetItem *tmp = listWidget->currentItem();
       QString nowitem = tmp->text();
       qDebug() << nowitem;

}
	if(!goodCount){
	   QDialog * setdialog = new QDialog;
	   QLabel * settmp = new QLabel;
	   settmp->setText(tr("该目录没有可打开的图片!"));
	   settmp->setStyleSheet("color:red;");
	   QTimer*timer = new QTimer(setdialog);
	   timer->start(1200);
	   connect(timer,SIGNAL(timeout()),setdialog,SLOT(close()));
	   QGridLayout *tmplayout = new QGridLayout;
	   tmplayout->addWidget(settmp,0,0,1,1);
	   setdialog->setLayout(tmplayout);
	   setdialog->setWindowFlags(Qt::FramelessWindowHint);
	   setdialog->exec();
}
 
 if (goodCount) {
     listWidget->setCurrentRow(nowCount);
     QListWidgetItem *tmp = listWidget->currentItem();
     QString nowitem = tmp->text();

/*
    QImage image(nowitem);
    if (image.isNull()) {
        qDebug("image is null");
        QMessageBox::information(this, tr("Image Viewer"),
                                 tr("Cannot load %1.").arg(fileName));
        return;
*/
		if(tmp->text().contains(".gif"))
		   {
		   		pNextButton		->setEnabled(true);
    			pPreviouButton	->setEnabled(true);
				pDeskButton     ->setEnabled(true);
			    pZinButton		->setEnabled(false);
			    pZoutButton		->setEnabled(false);
			    pNorButton		->setEnabled(false);
			    pFitButton		->setEnabled(false);

		   		m_movie->stop();
				qDebug()<<tmp->text()<<" is gif!";
				
				m_movie->setFileName(tmp->text());
	 
	    		pimageLabel->setMovie(m_movie);
	    		m_movie->start();

		   }else{

				qDebug()<<tmp->text()<<" is not gif!";
				m_movie->stop();

				pNextButton		->setEnabled(true);
    			pPreviouButton	->setEnabled(true);
				pDeskButton     ->setEnabled(true);
				pZinButton		->setEnabled(true);
			    pZoutButton		->setEnabled(true);
			    pNorButton		->setEnabled(true);
			    pFitButton		->setEnabled(true);
				
	       		QImage image(nowitem);
	        	if (image.isNull()) {
	            	QMessageBox::information(this, tr("Image Viewer"),
	                                     tr("Cannot load %1.").arg(dirCurrent->absoluteFilePath(*currentFile)));
	                           //          tr("Cannot load %1.").arg(fileName));
	            	return;
	        	}
				
	        	pimageLabel->setPixmap(QPixmap::fromImage(image));
	        	m_scaleFactor = 1.0;
	   		}
	  
    	}

    m_flag=1;
 	fullscreen_flag = 1;

    displayFiles = dirCurrent->entryList(supportFormat, QDir::Files);
    currentFile = displayFiles.constBegin();

}

void PictureWidget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
        startPos = event->pos();
}

void PictureWidget::mouseMoveEvent(QMouseEvent *event)
{
     if(m_flag)
    if (event->buttons() & Qt::LeftButton) {
        int distance = (event->pos().x() - startPos.x());
        if (distance >= 100) {
            countnet2++;
            if(countnet2==2){
                        if(m_flag)
                        on_actionNext_triggered();
                        countnet2=0;
            }
            pOpenFileButton		->setEnabled(true);
            pWcloseButton		    ->setEnabled(true);
            pDeskButton      ->setEnabled(true);

            pOpenFileButton		->setVisible(true);
            pWcloseButton		    ->setVisible(true);
            pDeskButton      ->setVisible(true);
            pZinButton		->setVisible(true);
            pZoutButton		->setVisible(true);
            pNorButton		->setVisible(true);
            pNextButton		->setVisible(true);
            pPreviouButton	->setVisible(true);
            pFitButton		->setVisible(true);
            m_pTimer->stop();

        }
        if (distance <= -100) {
            countnet2++;
                if(countnet2==2){
                        if(m_flag)
                on_actionPrevious_triggered();
                countnet2=0;
               }
                pOpenFileButton		->setEnabled(true);
                pWcloseButton		    ->setEnabled(true);
                pDeskButton      ->setEnabled(true);

                pOpenFileButton		->setVisible(true);
                pWcloseButton		    ->setVisible(true);
                pDeskButton      ->setVisible(true);
                pZinButton		->setVisible(true);
                pZoutButton		->setVisible(true);
                pNorButton		->setVisible(true);
                pNextButton		->setVisible(true);
                pPreviouButton	->setVisible(true);
                pFitButton		->setVisible(true);
                m_pTimer->stop();

        }
    }
}

void PictureWidget::pictureClose()
{
    close();
}

void PictureWidget::desk()
{
    pOpenFileButton	->setVisible(false);
    pOpenFileButton ->setVisible(false);
    pZinButton		->setVisible(false);
    pZoutButton		->setVisible(false);
    pNorButton		->setVisible(false);
    pNextButton		->setVisible(false);
    pPreviouButton	->setVisible(false);
    pFitButton		->setVisible(false);
    pWcloseButton	->setVisible(false);
    pDeskButton		->setVisible(false);
    deskflag=1;

	QDialog * setdialog = new QDialog;
	QLabel * settmp = new QLabel;

	settmp->setText(tr("滑屏可调出工具栏"));
    settmp->setStyleSheet("color:green;");

	QTimer*timer = new QTimer(setdialog);
    timer->start(3000);
    connect(timer,SIGNAL(timeout()),setdialog,SLOT(close()));

	QGridLayout *tmplayout = new QGridLayout;

	tmplayout->addWidget(settmp,0,0,2,2);
    setdialog->setLayout(tmplayout);
    setdialog->setWindowFlags(Qt::FramelessWindowHint);
    setdialog->exec();
//    m_pTimer->start();    //关闭自动播放

}

void PictureWidget::on_actionNext_triggered()
{
    pScrollArea->setWidgetResizable(0);

    if (goodCount) {
       nowCount++;
       if(nowCount==goodCount)
           nowCount=0;

	   listWidget->setCurrentRow(nowCount);
       QListWidgetItem* tmp = listWidget->currentItem();
       QString nowitem = tmp->text();

	   qDebug()<<"tmp->text():"<<tmp->text();

	   if(tmp->text().contains(".gif"))
	   {
		    pZinButton		->setEnabled(false);
		    pZoutButton		->setEnabled(false);
		    pNorButton		->setEnabled(false);
		    pFitButton		->setEnabled(false);

	   		m_movie->stop();
			qDebug()<<tmp->text()<<" is gif!";
			
			m_movie->setFileName(tmp->text());
 
    		pimageLabel->setMovie(m_movie);
    		m_movie->start();

	   }else{
//	   		QImage image(nowitem);
			QImage image(tmp->text());


			qDebug()<<tmp->text()<<" is not gif!";
			m_movie->stop();
			
			pZinButton		->setEnabled(true);
		    pZoutButton		->setEnabled(true);
		    pNorButton		->setEnabled(true);
		    pFitButton		->setEnabled(true);
			
       		

        	if (image.isNull()) {
            	QMessageBox::information(this, tr("Image Viewer"),
                           //          tr("Cannot load %1.").arg(dirCurrent->absoluteFilePath(*currentFile)));
                                     tr("Cannot load %1").arg(tmp->text()));
            	return;
        	}
			
        	pimageLabel->setPixmap(QPixmap::fromImage(image));
        	m_scaleFactor = 1.0;
	   	}

    }
//   qDebug()<<dirCurrent->absoluteFilePath(*currentFile);
}
void PictureWidget::on_actionPrevious_triggered()
{
    pScrollArea->setWidgetResizable(0);

    if (goodCount) {
    	qDebug("nowCount=%d",nowCount);
   
        nowCount--;

      	if(nowCount==-1)
        	nowCount=goodCount-1;

      	listWidget->setCurrentRow(nowCount);
		QListWidgetItem*tmp=listWidget->currentItem();
		QString nowitem = tmp->text();

		if(tmp->text().contains(".gif"))
		{
			pNextButton		->setEnabled(true);
			pPreviouButton	->setEnabled(true);
			pDeskButton     ->setEnabled(true);
			pZinButton 	 ->setEnabled(false);
			pZoutButton	 ->setEnabled(false);
			pNorButton 	 ->setEnabled(false);
			pFitButton 	 ->setEnabled(false);

			m_movie->stop();
			qDebug()<<tmp->text()<<" is gif!";

			m_movie->setFileName(tmp->text());

			pimageLabel->setMovie(m_movie);
			m_movie->start();		 
		}else{
		 
			qDebug()<<tmp->text()<<" is not gif!";
			m_movie->stop();

			pNextButton 	->setEnabled(true);
			pPreviouButton	->setEnabled(true);
			pDeskButton 	->setEnabled(true);
			pZinButton 	 ->setEnabled(true);
			pZoutButton	 ->setEnabled(true);
			pNorButton 	 ->setEnabled(true);
			pFitButton 	 ->setEnabled(true);

			QImage image(nowitem);
			if (image.isNull()) {
			 QMessageBox::information(this, tr("Image Viewer"), tr("Cannot load %1.").arg(dirCurrent->absoluteFilePath(*currentFile)));
			 return;
			}
			pimageLabel->setPixmap(QPixmap::fromImage(image));
			m_scaleFactor = 1.0;
		}

    }
}
void PictureWidget::DisplayImage(const QString &fileName)
{
        QImage image(fileName);
        if (image.isNull()) {
            QMessageBox::information(this, tr("Image Viewer"),
                                     tr("Cannot load %1.").arg(fileName));
            return;
        }
        pimageLabel->setPixmap(QPixmap::fromImage(image));
        scaleImage(1.0);
}

void PictureWidget::zoomIn()
{
    pScrollArea->setWidgetResizable(0);
    scaleImage(1.1);
}
void PictureWidget::zoomOut()
{
     pScrollArea->setWidgetResizable(0);
      scaleImage(0.9);
}

void PictureWidget::normalSize()
{
    pScrollArea->setWidgetResizable(0);
    pimageLabel->adjustSize();
    m_scaleFactor = 1.0;
	fullscreen_flag = 0;   //不是全屏
}
void PictureWidget::fitToWindow()
{
    //bool fitToWindow = fitToWindowAct->isChecked();
   // scrollArea->setWidgetResizable(fitToWindow);
    pScrollArea->setWidgetResizable(1);
	m_scaleFactor = 1.0;
	fullscreen_flag = 1;

}

void PictureWidget::scaleImage(double factor)
{
    Q_ASSERT(pimageLabel->pixmap());
    m_scaleFactor *= factor;

	if(fullscreen_flag)  
		pimageLabel->resize(m_scaleFactor * QSize(800, 480));
	else
		pimageLabel->resize(m_scaleFactor * pimageLabel->pixmap()->size());

    adjustScrollBar(pScrollArea->horizontalScrollBar(), factor);
    adjustScrollBar(pScrollArea->verticalScrollBar(), factor);

}
void PictureWidget::adjustScrollBar(QScrollBar *scrollBar, double factor)
{
    scrollBar->setValue(int(factor * scrollBar->value()
                            + ((factor - 1) * scrollBar->pageStep()/2)));
}

PictureWidget::~PictureWidget()
{
	m_movie->stop();
	delete m_movie;
}
